home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / os9.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-15  |  4.9 KB  |  216 lines

  1. /* $Id: os9.;
  2.     }
  3.     dotdot = d/04/15 19:22:52 lhecking Exp $ */
  4.  
  5. /* GNUPLOT - os9.c */
  6.  
  7. /*[
  8.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted,
  12.  * provided that the above copyright notice appear in all copies and
  13.  * that both that copyright notice and this permission notice appear
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the complete modified source code.  Modifications are to
  18.  * be distributed as patches to the released version.  Permission to
  19.  * distribute binaries produced by compiling modified sources is granted,
  20.  * provided you
  21.  *   1. distribute the corresponding source modifications from the
  22.  *    released version in the form of a patch file along with the binaries,
  23.  *   2. add special version identification to distinguish your version
  24.  *    in addition to the base release version number,
  25.  *   3. provide your name and address as the primary contact for the
  26.  *    support of your modified version, and
  27.  *   4. retain our contact information in regard to use of the base
  28.  *    software.
  29.  * Permission to distribute the released version of the source code along
  30.  * with corresponding source modifications in the form of a patch file is
  31.  * granted with same provisions 2 through 4 for binary distributions.
  32.  *
  33.  * This software is provided "as is" without express or implied warranty
  34.  * to the extent permitted by applicable law.
  35. ]*/
  36.  
  37.  
  38. /*
  39.  * Some Unix like functions that gnuplot uses.
  40.  * Original sources from the blars lib.
  41.  */
  42.  
  43. #include <stdio.h>
  44. #include <modes.h>
  45. #include <direct.h>
  46. #include <sgstat.h>
  47.  
  48. #ifdef PIPES
  49.  
  50. /* Original version by Robert A. Larson */
  51. /* Adapted by M.N. Schipper */
  52.  
  53. #include <string.h>
  54. #include <module.h>
  55.  
  56. extern char *_environ;
  57. extern int os9fork();
  58. extern mh_com *modlink();
  59. extern mh_com *modloadp();
  60.  
  61. static int proc[_NFILE];
  62. static mh_com *loadmods[_NFILE];
  63.  
  64. FILE *popen(command, mode)
  65. char *command;
  66. char *mode;
  67. {
  68.     int temp, fd;
  69.     FILE *pipe;
  70.     char *argv[4];
  71.     register char *cp;
  72.     mh_com *mod;
  73.     int linked = 0;
  74.  
  75.     if(mode[1]!='\0' || (*mode!='r' && *mode!='w')) return (FILE *)NULL;
  76.     fd = (*mode=='r');
  77.     if((temp = dup(fd)) <= 0) return (FILE *)NULL;
  78.     if((pipe = fopen("/pipe", "r+")) == NULL) {
  79.         close(temp);
  80.         return (FILE *)NULL;
  81.     }
  82.     close(fd);
  83.     dup(fileno(pipe));
  84.  
  85.     if (strrchr (command, '/') == NULL)
  86.         mod = modlink (command, 0);
  87.     else
  88.         mod = (mh_com *) -1;
  89.     if (mod == (mh_com *) -1)
  90.         loadmods[fileno(pipe)] = mod = modloadp (command, 0, NULL);
  91.     else
  92.     {
  93.         linked = 1;
  94.         loadmods[fileno(pipe)] = (mh_com *) -1;
  95.     }
  96.         
  97.     argv[0] = "shell";
  98.     if (mod != (mh_com *) -1)
  99.     {
  100.         argv[1] = "ex";
  101.         argv[2] = command;
  102.         argv[3] = (char *)NULL;
  103.     }
  104.     else
  105.     {
  106.         argv[1] = command;
  107.         argv[2] = (char *)NULL;
  108.     }
  109.     if((proc[fileno(pipe)] = os9exec(os9fork, argv[0], argv, _environ, 0, 0)) < 0)
  110.     {
  111.         fclose(pipe);
  112.         pipe = NULL;
  113.     }
  114.     close(fd);
  115.     dup(temp);
  116.     close(temp);
  117.     if (linked && mod != (mh_com *) -1)
  118.         munlink (mod);
  119.     return pipe;
  120. }
  121.  
  122. int pclose(pipe)
  123. FILE *pipe;
  124. {
  125.     int p, stat, w;
  126.  
  127.     if((p = proc[fileno(pipe)]) <= 0) return -1;
  128.     proc[fileno(pipe)] = 0;
  129.     fflush(pipe);
  130.     if (loadmods[fileno(pipe)] != (mh_com *) -1)
  131.         munlink (loadmods[fileno(pipe)]);
  132.     fclose(pipe);
  133.     while((w=wait(&stat)) != -1 && w!=p) {}
  134.     return w==-1 ? -1 : stat;
  135. }
  136.  
  137. #endif    /* PIPES */
  138.  
  139.  
  140. int isatty(f)
  141. int f;
  142. {
  143.     struct sgbuf sgbuf;
  144.  
  145.     if(_gs_opt(f, &sgbuf) < 0) return -1;
  146.     return sgbuf.sg_class == 0;
  147. }
  148.  
  149.  
  150. char *getwd(p)
  151. char *p;
  152. {
  153.     register char *cp;
  154.     register struct dirent *dp;
  155.     register int l, olddot = 0, i, d, dot, dotdot;
  156.     struct dirent db[8];
  157.     char buf[1024];
  158.  
  159.     cp = &buf[1024-1];
  160.     *cp = '\0';
  161.     for(;;) {
  162.     if((d = open(".", S_IREAD | S_IFDIR)) < 0) {
  163.         if(*cp) chdir(cp+1);
  164.         return NULL;
  165.     }
  166.     if((i = read(d, (char *)db, sizeof(db))) == 0) {
  167.         if(*cp) chdir(cp+1);
  168.         close(d);
  169.         return NULL;
  170.     }
  171.     dotdot = db[0].dir_addr;
  172.     dot = db[1].dir_addr;
  173.     if(olddot) {
  174.         i -= 2 * sizeof(struct dirent);
  175.         dp = &db[2];
  176.         for(;;) {
  177.             if(i <= 0) {
  178.             if((i = read(d, (char *)db, sizeof(db))) == 0) {
  179.             if(*cp) chdir(cp+1);
  180.             close(d);
  181.             return NULL;
  182.             }
  183.             dp = &db[0];
  184.         }
  185.         if(olddot == dp->dir_addr) {
  186.             l = strlen(dp->dir_name);
  187.             /* last character has parity bit set... */
  188.             *--cp = dp->dir_name[--l] & 0x7f;
  189.             while(l) *--cp = dp->dir_name[--l];
  190.             *--cp = '/';
  191.             break;
  192.         }
  193.         i -= sizeof(struct dirent);
  194.         dp++;
  195.         }
  196.     }
  197.     if(dot==dotdot) {
  198.         if(*cp) chdir(cp+1);
  199.         *p = '/';
  200.         if(_gs_devn(d, p+1) < 0) {
  201.             close(d);
  202.         return NULL;
  203.         }
  204.         close(d);
  205.         strcat(p, cp);
  206.         return p;
  207.     }
  208.     close(d);
  209.     if(chdir("..") != 0) {
  210.         if(*cp) chdir(cp+1);
  211.         return NULL;
  212.     }
  213.     olddot = dot;
  214.     }
  215. }
  216.